<?php
class ShareService extends BaseDbService
{
	public function __construct()
	{
		$this->dao	= new BaseDao('share');
	}
	
	public function getListByIds($ids)
	{
		if (empty($ids))
		{
			return array();
		}
		$result = $this->rows(array('id'=>array('in'=>$ids)));
		$list = array();
		if ($result)
		foreach($result as $item){
			$list[$item['id']] = $item;
			unset($list[$item['id']]['id']);
		}
		unset($result);
		return $list;
	}
	
	
	public function query($con,$page=1,$pageSize=20)
	{
		$api = new Api('share');
		if(array_key_exists('tag', $con))
		{
			//TODO 解析标签
			//预留，暂时不用
		}
		
		$page = intval($page);
		$pageSize = intval($pageSize);
		if($page<1)
		{
			$page = 1;
		}
		if($pageSize>40)
		{
			$pageSize = 40;
		}
		$start = ($page-1)*$pageSize;
		
		$result = $api->fetchRows('',$con,'','',$pageSize,$start);
		
		//关联用户
		if(!empty($result['list']))
		{
			$userIds = Util::parseIds('user_id', $result['list']);
			if(!empty($userIds))
			{
				//TODO 不建议service如此调用
				$userService = new UserService();
				$users = $userService->getListByIds($userIds);
			}
			$baseUrl = C('LtConfig')->get('baseurl');
			foreach($result['list'] as &$lv)
			{
				if(array_key_exists($lv['user_id'], $users))
				{
					$lv['username'] = $users[$lv['user_id']]['username'];
				}
				else
				{
					$lv['username'] = '匿名用户';
				}
				$lv['logo'] = $baseUrl.'images/1.jpg';
			}
		}
		
		return $result;
	}
	
	public function getDetail($id)
	{
		$api = new Api('share');
		$result = $api->fetch('',array('id'=>$id));
		
		$info = $result['item'];
		if($info)
		{
			if($info['user_id'])
			{
				//TODO 不建议service如此调用
				$userService = new UserService();
				$user = $userService->get($info['user_id']);
			}
			else
			{
				$user = array('username'=>'匿名用户');
			}
			
			$baseUrl = C('LtConfig')->get('baseurl');
			$user['logo'] = $baseUrl.'images/1.jpg';
			
			$info['user'] = $user;
		}
		
		
		return $info;
	}
}